#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ifstream;

#define ERROR_CHECK(cond, msg, cleanup) \
    do{ \
        if(cond) { \
            std::cerr << "Error:" << msg << "(" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \
            cleanup; \
            return; \
        } \
    }while(0)

struct Record
{
    string _word;
    int _frequency;
};

class Dictionary
{
public:
    void read(const std::string &filename);
    void store(const std::string &filename);
private:
    void sortedInsert(string &vcb);
    vector<Record> _dict;
};

void Dictionary::sortedInsert(string &str){
    auto it = lower_bound(_dict._word.begin(), _dict._word.end(), str);
    _dict.insert(it, str);
    _dict[it]._frequency++;
}

void Dictionary::read(const std::string &filename){
    // 1. 打开文件
    ifstream ifs(filename);
    ERROR_CHECK(!ifs.good(), "ifstream is not good", NULL);

    string word;
    while(ifs >> word){
        sortedInsert(word);
    }

    for(int i = 0; i < _dict.size(); i++){
        cout << _dict._word << "." << _dict._frequency << endl;
    }

    cout << endl;
    ifs.close();
}

int main(){
    Dictionary Dec;

    Dec.read("The_Holy_Bible.txt");
}
